import json

import requests
import unittest
from parameterized import parameterized
from read_data import read_data


def data():
    list_data = []
    data_url = './data/login.json'
    with open(data_url, 'r', encoding='utf-8') as f:
        json_data = json.load(f)
        print(json_data)
        for case_data in json_data:
            username = case_data.get('username')
            password = case_data.get('password')
            verify_code = case_data.get('verify_code')
            status_code = case_data.get('status_code')
            msg = case_data.get('msg')
            status = case_data.get('status')
            list_data.append((username, password, verify_code, status_code, msg, status))
        return list_data


class TPShopLoginParameter(unittest.TestCase):
    def setUp(self):
        self.session = requests.Session()
        self.verify = 'http://192.168.0.100/index.php?m=Home&c=User&a=verify'
        self.login = 'http://192.168.0.100/index.php?m=Home&c=User&a=do_login'

    def tearDown(self):
        self.session.close()

    @parameterized.expand(data())
    def test01_login(self, username, password, verify_code, status_code, msg, status):
        response = self.session.get(url=self.verify)
        self.assertEqual(200, response.status_code)
        self.assertIn('image', response.headers.get('Content-Type'))
        data_from = {"username": username, "password": password, "verify_code": verify_code}
        response = self.session.post(url=self.login, data=data_from)
        self.assertEqual(status_code, response.status_code)
        self.assertIn(msg, response.json().get('msg'))
        self.assertEqual(status, response.json().get('status'))
